﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.Script.Serialization;
using System.Linq;

using MvcCms.Data;
using MvcCms.Service.Filters;
using MvcCms.Service.Code.Email;
using MvcCms.Service;
using MvcCms.Store.Web.ViewModels;
using MvcCms.Data.Entities;
using MvcCms.Service.Code.Encryption;
using MvcCms.Service.Code;

using MvcCms.Store.Service;
using AutoMapper;
using MvcCms.Service.Cache;

namespace MvcCms.Store.Web.Controllers
{
    [RequireSsl(Redirect = true)]  
    public class StoreController : MvcCms.Service.Code.BaseClasses.CMSControllerBase
    {
        #region Constructors
        private readonly IStoreService _storeService;
        private readonly ICacheService _cacheService;
        private readonly IAccountService _accountService;
        public StoreController(ICacheService cacheService, IStoreService storeService, IAccountService accountService, IZoneService zoneService, ICmsService cmsService)
            : base(cmsService, zoneService, accountService)
        {
            _storeService = storeService;
            _cacheService = cacheService;
            _accountService = accountService;
        }

        #endregion

        [AuthorizeStore]              
        public ActionResult MyOrders()
        {
            var portal = _cacheService.GetCurrentPortal();
            var user = _accountService.GetUser();
            var orders = _storeService.ListOrders(user.UserID);

            var model = new DefaultViewModel
            {
                _Portal = portal,
                _Orders = orders
            };
            ViewBag.Title = portal.Name;
            return View(model);
        }

        public ActionResult Index()
        {
            var portal = _cacheService.GetCurrentPortal();
            var categories = _storeService.ListCategories(portal.PortalID);

            var model = new DefaultViewModel
            {
                _Portal = portal,
                _Categories = categories
            };
            ViewBag.Title = portal.Name;
            return View(model);
        }

        public ActionResult RefundPolicy()
        {
            var portal = _cacheService.GetCurrentPortal();

            var model = new DefaultViewModel
            {
                _Portal = portal
            };
            ViewBag.Title = portal.Name + " Refund Policy";
            return View(model);
        }

        public ActionResult UserPolicy()
        {
            var portal = _cacheService.GetCurrentPortal();

            var model = new DefaultViewModel
            {
                _Portal = portal
            };
            ViewBag.Title = portal.Name + " User Policy";
            return View(model);
        }

        public ActionResult PrivacyPolicy()
        {
            var portal = _cacheService.GetCurrentPortal();

            var model = new DefaultViewModel
            {
                _Portal = portal
            };
            ViewBag.Title = portal.Name + " Privacy Policy";
            return View(model);
        }

        public ActionResult ContactUs()
        {
            var portal = _cacheService.GetCurrentPortal();

            var model = new DefaultViewModel
            {
                _Portal = portal
            };
            ViewBag.Title = portal.Name + " Contact Us";
            return View(model);
        }

        public ActionResult Browse(string category)
        {
            var portal = _cacheService.GetCurrentPortal();
            var categoryToShow = _storeService.GetCategory(category, portal.PortalID);
            var products = categoryToShow.Products.ToList();

            var model = new DefaultViewModel
            {
                _Category = categoryToShow,
                _Portal = portal,
                _Products = products
            };
            ViewBag.Title = portal.Name;
            return View(model);
        }

        public ActionResult Details(int id)
        {
            var portal = _cacheService.GetCurrentPortal();
            var product = _storeService.GetProduct(id);

            Mapper.CreateMap<StoreProduct, ProductViewModel>();
            var productModel = Mapper.Map<StoreProduct, ProductViewModel>(product);

            var model = new DefaultViewModel
            {
                _Portal = portal,
                _Product = productModel
            };
            ViewBag.Title = portal.Name;
            return View(model);
        }

        [ChildActionOnly]
        public ActionResult CategoryMenu()
        {
            var portal = _cacheService.GetCurrentPortal();
            var categories = _storeService.ListCategories(portal.PortalID);

            return PartialView(categories);
        }

    }
}